package com.woniuxy.controller;

import com.woniuxy.entity.Drug;
import com.woniuxy.service.DrugService;
import com.woniuxy.util.MinioUtils;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/drug")
public class DrugController {

    @Resource
    private DrugService drugService;

    @Resource
    private MinioUtils minioUtils;

    @PostMapping("/queryList")
    public ResponseData<?> queryList(@RequestBody PageParam<Drug> pageParam){
        Assert.notNull(pageParam,"pageParam is not null");
        return drugService.queryList(pageParam);
    }
    @PostMapping("/upload")
    public ResponseData<?> uploadDrugImage(MultipartFile file) throws Exception {
        return minioUtils.uploadFile(file,"image");
    }

    @GetMapping("/getToken")
    public ResponseData<?> getToken() {
        return drugService.generateToken();
    }


    @PostMapping("/addDrug")
    public ResponseData<?> addDrug(@RequestBody Drug drug){
        Assert.notNull(drug,"drug is not null");
        return drugService.addDrug(drug);
    }

    @GetMapping("/queryDrug/{id}")
    public ResponseData<?> queryDrug(@PathVariable("id")Integer id){
        Assert.notNull(id,"id is not null");
        return drugService.queryDrugById(id);
    }

    @PostMapping("/updateDrug")
    public ResponseData<?> updateDrug(@RequestBody Drug drug){
        Assert.notNull(drug,"drug is not null");
        return drugService.updateDrug(drug);
    }

    @GetMapping("/deleteDrug/{id}")
    public ResponseData<?> deleteDrug(@PathVariable("id")Integer id){
        Assert.notNull(id,"id is not null");

        return drugService.deleteDrug(id);
    }

    @PostMapping("/queryDrugList")
    public ResponseData<?> queryDrugList(@RequestBody Drug drug){
        Assert.notNull(drug,"drug is not null");
        return drugService.queryDrugList(drug);
    }
}
